//
//  ColectionViewController.m
//  UI12豆瓣大项目
//
//  Created by 李奇 on 15/11/4.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ColectionViewController.h"
#import "ActivitySinglton.h"
#import "Activity.h"
#import "DetailViewController.h"

@interface ColectionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) NSMutableArray *CollectArray;

@property (nonatomic,retain) UITableView *myTableV;


@end

@implementation ColectionViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //刷新cell
    [self.myTableV reloadData];
    /*
     
    //出栈用的不对
    [self.navigationController popToRootViewControllerAnimated:YES];
     
     */
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myTableV = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableV.delegate = self;
    self.myTableV.dataSource = self;
    [self.view addSubview:self.myTableV];
    [self.myTableV release];
    
    [self.myTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //去掉分割线
    self.myTableV.tableFooterView = [UIView new];
    self.CollectArray = [NSMutableArray array];
    
    //右导航编辑
    self.navigationItem.rightBarButtonItem = self.editButtonItem
    ;
    // Do any additional setup after loading the view.
}

//右导航逻辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.myTableV setEditing:editing animated:YES];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //
        [[ActivitySinglton shareDataBaseSingleton] openDB];
        [[ActivitySinglton shareDataBaseSingleton] createCollectionTable];
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
        if (name != nil) {
#warning 最好复制一份,保持好习惯
            self.CollectArray = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:name];
            Activity *activity = [self.CollectArray objectAtIndex:indexPath.row];
            //删除
            [self.CollectArray removeObject:activity];
            [[ActivitySinglton shareDataBaseSingleton] deleteCollection:activity.title];
            //刷新界面
            [self.myTableV reloadData];
            NSLog(@"删除试试");
        }
    }
}


//修改Deletail
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"不要了";
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    self.CollectArray = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:name];
    return self.CollectArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    
    //    Activity *activityy = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"]];// 不能只写一个, 要用数组
    
    [[ActivitySinglton shareDataBaseSingleton] openDB];
    [[ActivitySinglton shareDataBaseSingleton] createCollectionTable];
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    
#warning 空的name也能参与查找!? 在数据库列表能看出来
    if (name != nil) {
        self.CollectArray = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:name];
        //        NSLog(@"卡看%@", self.CollectArray);
        //        NSLog(@"kkkkkname %@", name);
        Activity *activity = [self.CollectArray objectAtIndex:indexPath.row];
        
        cell.textLabel.text = activity.title;
    }else {
        cell.textLabel.text = nil;
    }
    
    return cell;
}


//点击触发事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
#warning   对应!!!!
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    Activity *activity = [[[ActivitySinglton shareDataBaseSingleton] selectAllCollection] objectAtIndex:indexPath.row];
    detailVC.activity = activity;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
